package util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import ds.BugInfo;

public class WekaDataWrapper {
	public static void main(String[] args) {
		
	}
	public static void printAttributes(PrintStream arffStream, ArrayList<String> labels, List<String> wordList) {
		arffStream.println("@relation BugReports");
		arffStream.print("@attribute category {");
		
		int i=0;
		for(i = 0; i < labels.size()-1; i ++){
			if(!labels.get(i).equals(""))
				arffStream.print(labels.get(i) + ", ");
		}
		arffStream.print(labels.get(i));
		arffStream.println("}");
		
		for(String word : wordList){
			arffStream.println("@attribute " + word + " numeric");
		}
		arffStream.println("@data");
	}
	
	public static List<String> readWordList(String Wordlistfile) {
		List<String> wordList = new ArrayList<String>();
		BufferedReader wordReader = null;
		try {
			wordReader = new BufferedReader(new FileReader(Wordlistfile));
			for(String word = wordReader.readLine(); word != null; word = wordReader.readLine()){
				word = word.trim();
				wordList.add(word);
			}			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				wordReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return wordList;
	}
	
	public static void translateToArffTF(ArrayList<BugInfo> docs, ArrayList<String> labels, String Wordlistfile,String arffFile) {		
		PrintStream arffStream = null;
		try {
			arffStream = new PrintStream(arffFile);
			List<String> wordList = readWordList(Wordlistfile);
			
			printAttributes(arffStream, labels, wordList);
			
			for(BugInfo doc : docs) {
				arffStream.print("{");
				arffStream.print("0 " + doc.label);
				
				for(int i = 0; i < doc.termWeight.size(); i++){
					double value = doc.termWeight.get(i);
					if(value != 0.0){
						int attributeIndex = i + 1;
						arffStream.print(", " + attributeIndex + " " + value);
					}
				}
			
				arffStream.println("}");
			}
			System.out.println("arff translated!");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public static void translateToArffTF(ArrayList<BugInfo> docs, String label, String Wordlistfile,String arffFile) {		
		PrintStream arffStream = null;
		try {
			arffStream = new PrintStream(arffFile);
			List<String> wordList = readWordList(Wordlistfile);
			
			ArrayList<String> labels = new ArrayList<String>();
			labels.add(label);
			labels.add("non" + label);
			printAttributes(arffStream, labels, wordList);
			
			for(BugInfo doc : docs) {
				arffStream.print("{");
				if(doc.label.equals(label))
					arffStream.print("0 " + doc.label);
				else
					arffStream.print("0 " + "non" + label);
				
				for(int i = 0; i < doc.termWeight.size(); i++){
					double value = doc.termWeight.get(i);
					if(value != 0.0){
						int attributeIndex = i + 1;
						arffStream.print(", " + attributeIndex + " " + value);
					}
				}
			
				arffStream.println("}");
			}
			System.out.println("arff translated!");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}
